/**
 * Copyright (c) Jan 15, 2013 8:26:32 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.service.face.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.ArrayUtils;
import org.extblog.wkm.web.dao.face.BodyDao;
import org.extblog.wkm.web.dao.face.FaceDao;
import org.extblog.wkm.web.dao.face.HomePageDao;
import org.extblog.wkm.web.service.AbstractService;
import org.extblog.wkm.web.service.face.FaceEditService;
import org.extblog.wkm.web.vo.BodyItemFaceView;
import org.extblog.wkm.web.vo.HomePageFaceView;
import org.extblog.wkm.web.vo.LeftItemFaceView;
import org.springframework.stereotype.Component;

/**
 * @author WKM
 * FaceServiceImpl.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Component
public class FaceEditServiceImpl extends AbstractService implements FaceEditService {
	
	/** dao **/
	private FaceDao faceDao = null;
	
	private BodyDao bodyDao = null;
	
	private HomePageDao homePageDao = null;
	
	public HomePageDao getHomePageDao() {
		return homePageDao;
	}
	@Resource
	public void setHomePageDao(HomePageDao homePageDao) {
		this.homePageDao = homePageDao;
	}
	public FaceDao getFaceDao() {
		return faceDao;
	}
	@Resource
	public void setFaceDao(FaceDao faceDao) {
		this.faceDao = faceDao;
	}
	public BodyDao getBodyDao() {
		return bodyDao;
	}
	@Resource
	public void setBodyDao(BodyDao bodyDao) {
		this.bodyDao = bodyDao;
	}
	
	public boolean updateRootItem(LeftItemFaceView view) {
		// TODO Auto-generated method stub
		log.info("更新记录:" + view.getItemName() + ";ID=" + view.getItemId());
		int row = faceDao.updateItem(view);
		log.info("影响数据记录条数:" + row);
		return row >= 0 ;
	}

	public List<Map<String,String>> getBodyItems(BodyItemFaceView view) {
		// TODO Auto-generated method stub
		log.info("开始查询bodys....");
		if(view.getItemParent() == null){
			log.info("无法查询bodys.");
			return new ArrayList<Map<String,String>>();
		}
		List<Map<String,String>> list = bodyDao.selectBodys(view);
		if(list == null) {
			log.info("没有到查询bodys.");
			list = new ArrayList<Map<String,String>>();
		}
		
		log.info("查询bodys个数:" + list.size());
		return list;
	}
	
	public List<Map<String,String>> getRootItemsToMap() {
		// TODO Auto-generated method stub
		List<Map<String,String>> list = faceDao.getRootItems();
		if(list == null) {
			list = new ArrayList<Map<String,String>>();
		}
		List<LeftItemFaceView> views = converBean(list);
		if(views.size() == 0){
			log.info("没有查询到任何记录...");
		} else {
			log.info("查询到记录:" + ArrayUtils.toString(views));
		}
		return list;
	}
	
	public boolean removeRootById(String rootId) {
		// TODO Auto-generated method stub
		log.info("删除ID=" + rootId);
		int row = faceDao.deleteHomePage(rootId);
		log.info("影响数据记录条数:" + row);
		return row >= 0;
	}
	
	private List<LeftItemFaceView> converBean(List<Map<String,String>> list){
		
		List<LeftItemFaceView> views = new ArrayList<LeftItemFaceView>();
		for(Map<String,String> map:list){
			LeftItemFaceView view = new LeftItemFaceView();
			view.setItemId(map.get("itemId"));
			view.setItemName(map.get("itemName"));
			view.setItemParent(map.get("itemParent"));
			view.setItemSeq(map.get("itemSeq"));
			view.setItemType(map.get("itemType"));
			view.setItemDesc(map.get("itemDesc"));
			views.add(view);
		}
		return views;
	}
	
	public List<Map<String, String>> getChildItemsToMap() {
		// TODO Auto-generated method stub
		log.info("获取所有的子节点信息:");
		List<Map<String,String>> list = faceDao.getItems();
		if(list == null) {
			list = new ArrayList<Map<String,String>>();
		}
		List<LeftItemFaceView> views = converBean(list);
		if(views.size() == 0){
			log.info("没有查询到任何记录...");
		} else {
			log.info("查询到记录:" + ArrayUtils.toString(views));
		}
		return list;
	}

	
	public List<Map<String, String>> getChildItemsToMap(String parentId) {
		// TODO Auto-generated method stub
		log.info("根据ID=" + parentId + "获取所有的子节点信息:");
		List<Map<String,String>> list = faceDao.getItemsByParentId(parentId);
		if(list == null) {
			list = new ArrayList<Map<String,String>>();
		}
		List<LeftItemFaceView> views = converBean(list);
		if(views.size() == 0){
			log.info("没有查询到任何记录...");
		} else {
			log.info("查询到记录:" + ArrayUtils.toString(views));
		}
		return list;
	}

	public boolean updateBodyItem(BodyItemFaceView view) {
		// TODO Auto-generated method stub
		log.info("更新ID=" + view.getItemParent() + "地址信息:");
		int row = bodyDao.updateBody(view);
		log.info("影响数据记录条数:" + row);
		return row >= 0;
	}

	public boolean deleteBodyItem(BodyItemFaceView view) {
		// TODO Auto-generated method stub
		log.info("删除ID=" + view.getItemParent() + "地址信息:");
		int row = bodyDao.deleteBody(view);
		log.info("影响数据记录条数:" + row);
		return row >= 0;
	}

	public boolean deleteHomePage(HomePageFaceView view) {
		// TODO Auto-generated method stub
		log.info("删除ID=" + view.getUrlId() + "地址信息:");
		int row = homePageDao.deleteHomePage(view);
		log.info("影响数据记录条数:" + row);
		return row >= 0;
	}

	public List<Map<String, String>> loadHomePage() {
		// TODO Auto-generated method stub
		log.info("查询所有的首页地址信息:");
		return homePageDao.selectHomePage();
	}

	public boolean updateHomePage(HomePageFaceView view) {
		// TODO Auto-generated method stub
		log.info("更新ID=" + view.getUrlId() + "地址信息:");
		int row = homePageDao.updateHomePage(view);
		log.info("影响数据记录条数:" + row);
		return row >= 0;
	}
	
}
